package com.smsclient.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.smsclient.constant.GlobalConstant;
import com.smsclient.exception.BusinessException;
import com.smsclient.init.CommonFacade;
import com.smsclient.mapper.OrgTemplateRespository;
import com.smsclient.model.OrgTemplateActiveVo;
import com.smsclient.model.OrgTemplateQueryVo;
import com.smsclient.model.OrgTemplateViewVo;
import com.smsclient.model.entity.SmsOrgTemplate;
import com.smsclient.service.SmsOrgTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@Service
public class SmsOrgTemplateServiceImpl implements SmsOrgTemplateService {

    @Autowired
    private CommonFacade commonFacade;

    @Autowired
    private OrgTemplateRespository orgTemplateRespository;

    @Override
    public void saveOrgTemplate(SmsOrgTemplate orgTemplate) {
        if(StrUtil.isNotBlank(orgTemplate.getId())){
            commonFacade.freshUpdateData(orgTemplate);
        }else{
            commonFacade.freshCreateData(orgTemplate);
        }
        //todo 签名业务逻辑
        orgTemplateRespository.save(orgTemplate);
    }

    @Override
    public OrgTemplateActiveVo activeTemplateStatus(OrgTemplateActiveVo activeVo) throws BusinessException {
        Optional<SmsOrgTemplate> templateOptional = orgTemplateRespository.findById(activeVo.getId());
        if(!templateOptional.isPresent()){
            throw new BusinessException("sys_err");
        }
        SmsOrgTemplate template = templateOptional.get();
        if(template.getTamplateStatus() == activeVo.getTamplateStatus()){
            throw new BusinessException(2005,"模板状态重复,无需修改!");
        }
        BeanUtils.copyProperties(activeVo, template);
        template = orgTemplateRespository.save(template);
        BeanUtils.copyProperties(template,activeVo);
        return activeVo;
    }

    @Override
    public List findAll() {
        List list = orgTemplateRespository.findAll();
        return list;
    }

    @Override
    public Page listTamplate(OrgTemplateQueryVo vo) {
        SmsOrgTemplate template = new SmsOrgTemplate();
        BeanUtils.copyProperties(vo, template);
        Example<SmsOrgTemplate> example = Example.of(template);
        PageRequest pageRequest = new PageRequest(0, 10, GlobalConstant.CREATE_DATE_ESC_SORT);
        Page<SmsOrgTemplate> page = orgTemplateRespository.findAll(example, pageRequest);
        if(CollectionUtil.isNotEmpty(page.getContent())){
            List<OrgTemplateViewVo> viewList = page.getContent().stream().map(p -> {
                OrgTemplateViewVo viewVo = new OrgTemplateViewVo();
                BeanUtils.copyProperties(p, viewVo);
                return viewVo;
            }).collect(Collectors.toList());
            Page resultPage = new PageImpl(viewList);
            return resultPage;
        }
        return page;
    }

    @Override
    public SmsOrgTemplate getOrgTempalte(String orgId) {
        return null;
    }
}
